package org.yx.service.smat.release_material_batch_sn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.yx.exception.BizException;
import org.yx.models.SmatMsnSts;
import org.yx.models.SqcmHldDef;
import org.yx.models.SqcmRlsDef;
import org.yx.service.bas.create_rule_code.BasCreateRuleCodeService;
import org.yx.service.smat.insert_material_sn_history.SmatInsertMaterialSnHistoryIn;
import org.yx.service.smat.insert_material_sn_history.SmatInsertMaterialSnHistoryService;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;
import org.yx.utils.ServiceBeanUtil;

import com.xiaoleilu.hutool.collection.CollUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 批量解除物料批次锁定
 * 
 * @author zxw
 * @since 2019/03/30
 **/
public class SmatReleaseMaterialBatchSnService extends BaseService {
	private final static String RELEASE_NO = "MSN_RELEASE_NO";

	public SmatReleaseMaterialBatchSnOut releaseMaterialBatchSn(SmatReleaseMaterialBatchSnIn in) throws Exception {
		if (CollUtil.isEmpty(in.getMsnList())) {
			throw new BizException("SYS-0004", new FieldMap("parameter", "snList"));
		}

		String releaseNo = BasCreateRuleCodeService.createRuleCode(RELEASE_NO);
		List<SmatMsnSts> msnStsList = new ArrayList<SmatMsnSts>();
		Map<String, SqcmHldDef> mapHeldDef = new HashMap<String, SqcmHldDef>();
		for (SmatMsnSts msn : in.getMsnList()) {
			SmatMsnSts oldMsn = selectOneLocked(SmatMsnSts.class, in.getUserOrgCode(), msn.getMsn());
			if (oldMsn == null) {
				throw new BizException("SMAT-0005", "物料批号${msn}不存在或已经被删除。", FieldMap.of("msn", msn.getMsn()));
			}

			if (!"H".equals(oldMsn.getStatus())) {
				continue;
			}

			if (!mapHeldDef.containsKey(oldMsn.getHoldNo())) {
				mapHeldDef
						.put(oldMsn.getHoldNo(), selectWithPk(SqcmHldDef.class, oldMsn.getOrgCode(), oldMsn.getHoldNo()));
			}

			if (mapHeldDef.get(oldMsn.getHoldNo()) == null) {
				throw new BizException("SQCM-0002", "找不到锁定信息。${holdNo}", FieldMap.of("holdNo", oldMsn.getHoldNo()));
			}

			if (!StrUtil.isAllBlank(in.getReleasePwd(), mapHeldDef.get(oldMsn.getHoldNo()).getHoldPwd())
					&& !StrUtil.equals(in.getReleasePwd(), mapHeldDef.get(oldMsn.getHoldNo()).getHoldPwd())) {
				throw new BizException("SQCM-0009", new FieldMap("holdNo", oldMsn.getHoldNo()));
			}

			oldMsn.setStatus("C");
			oldMsn.setEventCode("RELEASE");
			oldMsn.setEventValue(releaseNo);
			oldMsn.setReleaseNo(releaseNo);
			oldMsn.setHoldNo(null);

			msnStsList.add(oldMsn);
		}

		if (msnStsList.size() == 0) {
			throw new BizException("SMAT-0004");// 找不到可用的msn信息。
		}

		updateBatch(msnStsList, "status,eventCode,eventValue,releaseNo,holdNo");

		SmatInsertMaterialSnHistoryIn msnHisIn = new SmatInsertMaterialSnHistoryIn();
		msnHisIn.setMsnStsList(msnStsList);
		ServiceBeanUtil.getBean(SmatInsertMaterialSnHistoryService.class).insertMaterialSnHistory(msnHisIn);

		SqcmRlsDef release = new SqcmRlsDef();
		release.setOrgCode(in.getUserOrgCode());
		release.setReleaseNo(releaseNo);
		release.setReleaseType('M');
		release.setHoldNo(CollUtil.getFirst(mapHeldDef.values()).getHoldNo());
		release.setReleaseTime(getCurrentTime());
		release.setReleaseUser(in.getUserToken().getUserId());

		insert(release);

		SmatReleaseMaterialBatchSnOut out = new SmatReleaseMaterialBatchSnOut();
		out.setReleaseNo(releaseNo);

		return out;
	}
}